var express = require('express');
var expressWs = require('express-ws');
var router = express.Router();
expressWs(router);
const clients = {};
const chats = [];
router.ws("/connect/:id",function(ws,req){
    let id = req.params.id;
    if(!clients[id]){
        clients[id] = ws;
    }
    ws.on("message",function(event){
        
        chats.push(JSON.parse(event));
        Object.keys(clients).forEach(key => {
            clients[key].send(JSON.stringify(chats));
        });
       
    });
});

module.exports = router;